<template>
  <el-dialog :title="this.id?'修改用户':'创建用户'" :visible="showDialog" width="700px" :destroy-on-close="true" @close="btnCannel">
    <!-- 表单 -->
    <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="企业名称" prop="shortName">
        <el-input v-model="formData.shortName" style="width:80%" placeholder="" />
      </el-form-item>
      <el-form-item label="所属公司" prop="company">
        <el-input v-model="formData.company" style="width:80%" placeholder="" />
      </el-form-item>
      <el-form-item label="" prop="">
        <span>https://www.tianyancha.com （在此可查询所属公司全称及简称）</span>
      </el-form-item>
      <el-form-item label="城市地区" prop="province">
        <!-- 省份 -->
        <el-select v-model="formData.province" style="width:40%" placeholder="请选择" @change="getProvince">
          <el-option v-for="item in provinces" :key="item" :label="item" :value="item" />
        </el-select>
        <!-- 城市 -->
        <el-select v-model="formData.city" style="width:40%" placeholder="请选择" @focus="getCitys" :disabled="!formData.province">
          <el-option v-for="item in citys" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="方向(企业标签)" prop="tags">
        <el-input v-model="formData.tags" style="width:80%" placeholder="" />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input type="textarea" v-model="formData.remarks" style="width:80%" placeholder="请输入"></el-input>
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-row type="flex" justify="center">
            <el-button size="small" @click="btnCannel">取消</el-button>
            <el-button type="primary" size="small" @click="btnOk">确定</el-button>
          </el-row>

        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addCompanys, reviseCompanys } from '@/api/hmmm/companys.js'
import { provinces, citys } from '@/api/hmmm/citys.js'

export default {
  name: 'AddCompanys',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    id: {
      type: Number
    }
  },
  data () {
    return {
      formData: {
        isFamous: true,
        shortName: '', // 企业简称
        company: '', // 所属公司
        province: '', // 省份
        city: '', // 城市
        tags: '', // 标签
        remarks: '' // 备注
      },
      rules: {
        shortName: [
          { required: true, message: '企业名称不能为空', trigger: 'blur' }
        ],
        company: [
          { required: true, message: '所属公司不能为空', trigger: 'blur' }
        ],
        province: [
          { required: true, message: '省份不能为空', trigger: 'blur' }
        ],
        tags: [
          { required: true, message: '标签不能为空', trigger: 'blur' }
        ],
        remarks: [
          { required: true, message: '备注不能为空', trigger: 'blur' }
        ]

      },
      provinces: [],
      citys: []

    }
  },
  created () {
    this.getProvince()
  },
  mounted () { },
  // 组件
  components: {},
  // 计算属性
  computed: {},
  // 侦听
  watch: {
    // province(){

    // }
  },
  methods: {
    // 确认按钮
    async btnOk () {
      await this.$refs.formData.validate()
      if (this.id) {
        this.formData.isFamous = true
        await reviseCompanys(this.formData)
        this.$parent.id = null
        this.$message.success('修改成功')
      } else {
        await addCompanys(this.formData)
        this.$message.success('添加成功')
      }

      this.$parent.getCompanysList()
      this.$parent.showDialog = false
    },
    // 取消按钮
    btnCannel () {
      this.$refs.formData.resetFields()
      this.$parent.showDialog = false
      this.$parent.id = null
    },
    getDepartments () {

    },
    // 获取城市
    getProvince () {
      this.provinces = provinces()
      this.getCitys()
      if (this.formData.province) {
        this.formData.city = this.citys[0]
      }
    },
    getCitys () {
      this.citys = citys(this.formData.province)
    }
  }
}
</script>

<style scoped lang='less'>

</style>
